import { _decorator, Component, Node, UITransform } from 'cc';
import { ScrollMap } from '../ScrollMap';
import { TouchAndMoveV2 } from '../TouchAndMoveV2';
import { MapController } from '../MapController';
import { buildUINodeEx } from '../NodeEx';
import { MapResource } from '../MapResource';
import { MapTileObject } from '../MapTileObject';
import { AnimeCharacter } from '../AnimeCharacter';
import { SpriteAnimeResource } from '../SpriteAnimeResource';
const { ccclass, property } = _decorator;

@ccclass('TestDirector')
export class TestDirector extends Component {

    mapRes:MapResource;
    
    animeRes:SpriteAnimeResource;

    touchAndMove:TouchAndMoveV2;

    scrollMap:ScrollMap;

    player:AnimeCharacter;

    @property(Node)
    worldNode:Node;

    @property
    initGridX = 0;

    @property
    initGridY = 0;

    @property
    checkSize = false;

    start() {
        //ui
        const worldUI = this.worldNode.getComponent(UITransform);
        const viewWidth = worldUI.contentSize.width;
        const viewHeight = worldUI.contentSize.height;
        const viewCenterX = Math.round(worldUI.anchorPoint.x*viewWidth);
        const viewCenterY = Math.round(worldUI.anchorPoint.y*viewHeight);
        //init res
        this.mapRes = this.getComponent(MapResource);
        this.mapRes.init();
        this.animeRes = this.getComponent(SpriteAnimeResource);
        this.animeRes.init();
        //init map
        const mapNode = buildUINodeEx();
        this.worldNode.addChild(mapNode);
        mapNode.addComponent(MapController);
        this.scrollMap = mapNode.addComponent(ScrollMap);
        this.scrollMap.form = "rhomboid";
        this.scrollMap.viewX = -viewCenterX;
        this.scrollMap.viewY = -viewCenterY;
        this.scrollMap.viewWidth = viewWidth*2;
        this.scrollMap.viewHeight = viewHeight*2;
        this.scrollMap.tileWidth = this.mapRes.mapInfo.tileWidth;
        this.scrollMap.tileHeight = this.mapRes.mapInfo.tileHeight;
        this.scrollMap.initGridX = this.initGridX;
        this.scrollMap.initGridY = this.initGridY;
        this.scrollMap.initX = 0;
        this.scrollMap.initY = 0;
        this.scrollMap.checkSize = this.checkSize;
        this.scrollMap.init();
        //init map tiles
        const [panel,tileNodes] = this.scrollMap.addGroundTiles(buildUINodeEx,1);
        tileNodes.foreach((grid,node)=>{
            const tile = node.addComponent(MapTileObject);
            tile.mapLayer = "tiles";
            tile.mapRes = this.mapRes;
            tile.init();
        });
        //init map objects
        const objNodes = this.scrollMap.addCustomTiles(buildUINodeEx,10);
        objNodes.foreach((grid,node)=>{
            const tile = node.addComponent(MapTileObject);
            tile.mapLayer = "objects";
            tile.mapRes = this.mapRes;
            tile.init();
        });
        //init player
        const initPixel = this.scrollMap.toPixel({x:this.initGridX,y:this.initGridY});
        const playerNode = this.scrollMap.addObj("player",initPixel,buildUINodeEx);
        this.player = playerNode.addComponent(AnimeCharacter);
        this.player.animeRes = this.animeRes;
        this.player.init();
        //init input
        this.touchAndMove = this.worldNode.getComponent(TouchAndMoveV2);
        this.touchAndMove.setMap(mapNode);
        this.touchAndMove.setPlayer(playerNode);
        this.touchAndMove.targetX = initPixel.x;
        this.touchAndMove.targetY = initPixel.y;
        this.touchAndMove.init();
        //log
        this.schedule(()=>{
            console.log("current position: "+this.touchAndMove.currentX+","+this.touchAndMove.targetY+".    current grid: "+this.scrollMap.viewGrid.grid.x+","+this.scrollMap.viewGrid.grid.y);
        },1);
    }

    update(deltaTime: number) {
        this.touchAndMove.render(deltaTime);
        this.scrollMap.render();
        this.player.render(deltaTime);
    }
}
